<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2008 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  $Source$
  $Revision$
  $Author$
  $Date$
**********************************************/
if (!defined('CPG_NUKE')) { exit; }
global $prefix, $db, $module_name, $currentlang, $mainindex, $userinfo, $adminindex, $MAIN_CFG;
if (!($MAIN_CFG['global']['admingraphic'] & 4)) return '';

$menucats = array();
$modquery = $lnkquery = '';

$cpgtpl->set_handle('menu_css', 'menu_main_css.html');
if (is_admin()) {
	global $CLASS;
	require_once(CORE_PATH.'classes/cpg_adminmenu.php');
	if (!defined('ADMIN_PAGES')) {
		$cpgtpl->assign_var('MENU_ADMIN_CSS', $CLASS['adminmenu']->display('all', 'cssmenu'));
	}
} else {
	$modquery = 'WHERE active=1 AND inmenu=1';
	$lnkquery = 'WHERE active=1';
	$view = array();
	$view[] = 0;
	if (is_user()) {
		$view[] = 1;
		foreach ($userinfo['_mem_of_groups'] AS $key => $value) {
			$view[] = $key+3;
		}
	} else {
		$view[] = 3;
	}
	$modquery .= ' AND view IN ('.implode(',', $view).')';
	$lnkquery .= ' AND view IN ('.implode(',', $view).')';
}

$uri = str_ireplace('&', '&amp;', URL::canonical(true));

$result = $db->query('SELECT cid as cat_id, name, pos, link_type, link FROM '.$prefix.'_modules_cat ORDER BY pos');
while ($cat = $result->fetch_assoc()) {
	$cattitle = defined($cat['name']) ? constant($cat['name']) : str_replace('_', ' ', $cat['name']);
	$cat['name'] = $cattitle;
	$cat['status'] = 'sblack';
	if ($uri && false !== stripos($uri, $cat['link'])) {
		$cat['status'] = 'sgreen';
	}
	$menucats[$cat['cat_id']] = $cat;
}
$result->free();
unset($cat);

$menucats[0] = array('cat_id' => 0, 'name' => 'Resources', 'pos' => -1, 'link_type' => 0, 'link' => '', 'status' => '');

$result = $db->query('(SELECT mid AS lid, title, active, view, inmenu, cat_id, -1 as link_type, pos, title as link FROM '.$prefix.'_modules '.$modquery.')
UNION (SELECT lid, title, active, view, 1 as inmenu, cat_id, link_type, pos, link FROM '.$prefix.'_modules_links '.$lnkquery.')
ORDER BY pos');
while ($row = $result->fetch_assoc()) {
	if (!isset($menucats[$row['cat_id']])) {
		$row['cat_id'] = 0;
		trigger_error('Menu menu link "'.$row['title'].'"\'s parent was lost.', E_USER_WARNING);
	}
	$rowtitle = defined($row['title']) ? constant($row['title']) : str_replace('_', ' ', $row['title']);
	$row['title'] = $rowtitle;
	$menucats[$row['cat_id']]['items'][$row['pos']] = $row;
}
$result->free();
foreach ($menucats as $c => $cat) {
	if (empty($cat['items']) && empty($cat['link'])) continue;
	$block = $cat['cat_id'] >= 0 ? 'catcontent' : 'nocatcontent';
	if ($cat['name'] == '_HOME') {
		$cat['link'] = '';
	} else if (!empty($cat['link'])) {
		if (!$cat['link_type']) {
			$cat['link'] = URL::index($cat['link']);
		} elseif ($cat['link_type'] == 2) {
			$cat['link'] .= '" target="_blank';
		}
	}
	if (!empty($cat['items'])) {
		foreach ($cat['items'] as $i => $item) {
			if ($uri && false !== stripos($uri, $item['link'])) {
				$cat['items'][$i]['status'] = 'sgreen';
				$cat['status'] = 'sgreen';
			}
			else {
				$cat['items'][$i]['status'] = 'sblack';
				if (!$item['active']) {
					$cat['items'][$i]['status'] = 'sred';
				} elseif ($item['active'] && !$item['inmenu']) {
					$cat['items'][$i]['status'] = 'sgray';
				}
			}
			if ($item['link_type'] <= 0) {
				$cat['items'][$i]['link'] = URL::index($item['link']);
			} elseif ($item['link_type'] == 2) {
				$cat['items'][$i]['link'] .= '" target="_blank';
			}
		}
	}
	$cpgtpl->assign_block_vars($block, array(
		'S_CATTITLE' => $cat['name'],
		'S_URL' => $cat['link'],
		'S_HAS_ITEMS' => !empty($cat['items']),
		'S_STATUS' => $cat['status']
	));
	if (!empty($cat['items'])) {
		foreach ($cat['items'] as $item) {
			$cpgtpl->assign_block_vars($block.'.item', array(
				'S_STATUS' => $item['status'],
				'S_URL' => $item['link'],
				'S_TITLE' => $item['title'],
			));
		}
	}
}

if (defined('ADMIN_PAGES') && is_admin()) {
	$cpgtpl->assign_var('MENU_ADMIN_CATS', $CLASS['adminmenu']->display('all', 'cssmenu'));
}